home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11176 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  44 lines

  1. Newsgroups: comp.lang.c
  2. Path: xara.net!SoNet!uknet!owl-uk!news
  3. From: Kenn@owl-uk.co.uk (Ken Nicolson)
  4. Subject: Re: rand ?
  5. Message-ID: <314da47e.28351406@hector>
  6. Sender: news@owl-uk.co.uk (News system)
  7. Organization: Office Workstations Limited
  8. X-Newsreader: Forte Agent .99d/32.182
  9. References: <4idjt4$4du@itsop2.its.brooklyn.cuny.edu> <314BB352.2734@ccis.com>
  10. Date: Mon, 18 Mar 1996 18:03:50 GMT
  11.  
  12. Derek Lund <wlund@ccis.com> wrote:
  13.  
  14. >Daniel Zielinski wrote:
  15. >> 
  16. >> I need to generate random numbers between 0 and 51, I tried rand() with srand(i)
  17. >> i changes constantly but the program never exits. looks like the random number
  18. >> is always the same.  any help would be apriciated... thanss
  19. > If you want a truely random number use something like this
  20. >
  21. >#define RANDOMIZE() srand(time(NULL)) /* time() is in time.h */
  22. >main()
  23. >{
  24. > int i ;
  25. > while(i != 0 )
  26. >  printf("%d\n",i = random(52));
  27. >}
  28. >
  29. > int random(int i)
  30. > {
  31. >  double x = RAND_MAX + 1.0 ;/* RAND_MAX is defined in stdlib.h */
  32. >  int y ; 
  33. >  RANDOMIZE();
  34. >  y = 1 + rand() * (i /x) ;
  35. >  return y ;
  36. > }
  37. >
  38. >Derek Lund
  39. Did you even bother to try compiling and running this? It is patently
  40. obvious that this will not work if you call random() more than once per
  41. second. Maybe you should try reading the FAQ too?
  42.  
  43. Ken
  44.